<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Settings extends CI_Controller
{
    /**
     * Admin settings controller
     * Handle all settings
     */
    function __construct() {
        parent::__construct();
        $this->load->model('init_model');
        $this->load->helper('cookie');
        $this->load->library('auth');
        $this->auth->restrict();
        $this->load->helper('form');
    }
    
    /**
     * Index Controller
     * Redirect to $this->main
     * @access  public
     */
    function index()
    {
        redirect('backend/settings/main');
    }
    
    /**
     * Main Settings
     * @access public
     */
    function main()
    {
        //$data['nav'] = '';
        $this->load->library('form_validation');
        $this->load->helper('form');
        
        $data['nav'] = 'settings';
		if ( ! $this->auth->check_level(1))
		{
			$data['not_allowed'] = TRUE;
			$this->init_model->display_template('content', $data, 'backend'); 
			return FALSE;
		}
        
        $this->form_validation->set_rules('site_name', 'lang:kb_site_title', 'required');
		$this->form_validation->set_rules('site_email', 'lang:kb_email', 'required|valid_email');
		$this->form_validation->set_rules('max_search', 'lang:kb_max_search', 'required|numeric');
		$this->form_validation->set_rules('cache_time', 'lang:kb_cache_time', 'required|numeric');
		$this->core_events->trigger('settings/validation');
		
		if ($this->form_validation->run() == FALSE) 
		{
			$this->init_model->display_template('settings/main', $data, 'backend');
		}
		else
		{
            //var_dump('<pre>',$_POST);exit;
			foreach ($_POST as $key => $value)
			{
				$data = array( 
					'value' => $this->security->xss_clean($value)
				);
				$this->db->where('short_name', $key);
				$this->db->update('settings', $data);
			}
			$this->revert('backend/settings/');
		}
    }
    
    // ------------------------------------------------------------------------
	
	/**
	* Revert
	*
	* Show a message and redirect the user
	* 
	* @access	public
	* @param	string -- The location to goto
	* @return	array
	*/
	function revert($goto)
	{
		$data['nav'] = 'settings';
		$data['goto'] = $goto;
		$this->init_model->display_template('content', $data, 'backend');
	}
}
/* End of file settings.php */
/* Location: ./application/controllers/backend/settings.php */ 
